﻿using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Codeflows.Tracking.BuiltIn
{
    public static class PersistanceHelpers
    {
        public static byte[] Pack(this IDictionary<string, object> arguments)
        {
            var ms = new MemoryStream();
            new BinaryFormatter().Serialize(ms, arguments);
            return ms.ToArray();
        }

        public static IDictionary<string, object> Unpack(this byte[] data)
        {
            if (data.Length == 0)
                return null;
            var ms = new MemoryStream(data);
            return (IDictionary<string, object>)new BinaryFormatter().Deserialize(ms);
        }
    }
}